import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient_0602 {
    private DatagramSocket socket = null;

    private String severIp;
    private int severPort;

    public UdpEchoClient_0602( String severIp, int severPort) throws SocketException {
        socket = new DatagramSocket();
        this.severPort = severPort;
        this.severIp = severIp;
    }


    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("->");
            if (!scanner.hasNext()) {
                break;
            }
            String request = scanner.next();
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),
                    request.getBytes().length,
                    InetAddress.getByName(severIp),
                    severPort);
            socket.send(requestPacket);


            DatagramPacket responsePacket = new DatagramPacket(new byte[1024],1024);
            socket.receive(responsePacket);

            String response = new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient_0602 udpEchoClient0602 = new UdpEchoClient_0602("127.0.0.1",9090);
        udpEchoClient0602.start();
    }
}
